package com.abaaba.project2.shiro.realm;

import com.abaaba.project2.bean.Admin;
import com.abaaba.project2.bean.AdminExample;
import com.abaaba.project2.bean.User;
import com.abaaba.project2.bean.UserExample;
import com.abaaba.project2.mapper.AdminMapper;
import com.abaaba.project2.mapper.PermissionMapper;
import com.abaaba.project2.mapper.UserMapper;
import com.abaaba.project2.shiro.Token.CustomToken;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        CustomToken token = (CustomToken) authenticationToken;

        if(token.getSource().equals("admin"))
        {   String username = token.getUsername();

            AdminExample adminExample = new AdminExample();
            AdminExample.Criteria criteria = adminExample.createCriteria();
            criteria.andUsernameEqualTo(username);

            List<Admin> admins = adminMapper.selectByExample(adminExample);
            if(admins.size() == 0)
            {
                return new SimpleAuthenticationInfo(null, null, this.getName());
            }

            return new SimpleAuthenticationInfo(username, admins.get(0).getPassword(), this.getName());
        }else if(token.getSource().equals("wx"))
        {
            String username = token.getUsername();

            UserExample userExample = new UserExample();
            UserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUsernameEqualTo(username);

            List<User> users = userMapper.selectByExample(userExample);
            if(users.size() == 0)
            {
                return new SimpleAuthenticationInfo(null, null, this.getName());
            }

            return new SimpleAuthenticationInfo(users.get(0), users.get(0).getPassword(), this.getName());

        }

        return null;
    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //principal就是SimpleAuthenticationInfo的第一个参数，是自己存的
        String username = (String) principalCollection.getPrimaryPrincipal();
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        //由于这个傻逼表设计的问题，用了一个varchar去维护多表关系，因此不可能用一个链接查询把permission
        //只能分次查询
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Integer[] roleIds = admins.get(0).getRoleIds();
        //分别查询每一个身份对应的permission，然后添加到List<String中
        List<String> permissions = new ArrayList<>();
        for (Integer roleId : roleIds) {
            List<String> subPermissions = permissionMapper.selectPermissionsByRoleId(roleId);
            permissions.addAll(subPermissions);
        }

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addStringPermissions(permissions);


        return authorizationInfo;
    }


}
